amApp.config(function($routeProvider,$locationProvider){
    $routeProvider
     	.when('/index', {
            templateUrl:'list.html',
            controller: 'indexController'
        })
        .when('/index/detail/:order_id', {
            templateUrl:'detail.html',
            controller: 'detailController'
        })
         .otherwise({
            redirectTo: '/index'
        });
});

function orderService($http, $location) {
	return {
		orderList: function(search, limit,page,app_id, fn) {
			var param = {
				limit: limit,
				page: page,
                card_number:(search.card_number != ''&& typeof search.card_number != 'undefined')?search.card_number:'',
                cardid:(search.cardid != ''&& typeof search.cardid != 'undefined')?search.cardid:''

			};
			baas_yl.call({
                'http': $http,
                'method': 'post',
                'uri': 'order/orderList/count',
                'data' : param,
                'success': function(d) {
					if (typeof fn == 'function') {
						fn(d);
					}
                }
       		 });
		}
		,laoList: function(search, app_id, fn) {
			var data = {
                card_number:(search.card_number != ''&& typeof search.card_number != 'undefined')?search.card_number:'',
                cardid:(search.cardid != ''&& typeof search.cardid != 'undefined')?search.cardid:''
			};
			baas_yl.call({
				'http': $http,
				'method': 'post',
				'uri': 'archives/archives/search',
				'data': data,
				'success': function(d) {
					if (typeof fn == 'function') {
						fn(d);
					}
				}
			});
		},
        cardInfo : function(search, limit,page,app_id, fn){
            var data = {
                card_number:(search.card_number != ''&& typeof search.card_number != 'undefined')?search.card_number:''
            };
            baas_yl.call({
                'http': $http,
                'method': 'post',
                'uri': 'archives/archives/getArchivesCardInfo',
                'data': data,
                'success': function(d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            })
        }
	};
}

amApp.factory('orderService', ['$http','$location', orderService]);
amApp.controller('indexController', ['$scope','$routeParams','$window','$location','$http','orderService',indexController]);
amApp.controller('detailController', ['$scope','$routeParams','$window','$location','$http','orderService',detailController]);

function indexController($scope,$routeParams,$window,$location,$http,orderService){
	$scope.selectedPage = 1;
	$scope.limit = 20;
	$scope.data = {
			selectedPage: $scope.selectedPage
		};
	$scope.searchInfo = function(){

        $scope.filter = {
            card_number:angular.element("#card_number").val(),

            cardid:angular.element("#id_card").val()
        }

        if(typeof $scope.filter.card_number == "undefined" || $scope.filter.card_number =="" ){
            alert("请输入会员卡号");
            return;
        }
        if(typeof $scope.filter.cardid == "undefined" || $scope.filter.cardid =="" ){
            alert("请输入身份证号");
            return;
        }
        orderService.laoList($scope.filter, $scope.app_id, function(json) {
            $scope.search= json['list'][0];
            if(json['count'] == 0 ){
                alert("系统没有找到相关老人信息，请检查输入信息是否有误");
            }else{
                $scope.getOrderList();
                $scope.getCardInfo();
            }
        });
        $scope.getOrderList = function(){
            orderService.orderList($scope.filter,$scope.limit, $scope.selectedPage,$scope.app_id, function(json) {
                $scope.list = json['list'];
                $scope.count = json['count'];
                $scope.currentPage = 1;
                $scope.getPage(json['count']);
                console.log(json);
            });
        }
        $scope.getCardInfo = function (){
            orderService.cardInfo($scope.filter,$scope.limit, $scope.selectedPage,$scope.app_id,function(json){
                $scope.search.fund = json['fund'];
            });
        }

	}
	//分页
	$scope.changePage = function(p) {
		$scope.data.selectedPage = p;
		orderService.orderList($scope.search,$scope.limit, p, $scope.app_id, function(json) {
			$scope.list = json.list;
			$scope.count = json.count;
			$scope.currentPage = p;
			$scope.getPage(json.count);
		});
	};
	$scope.getPage = function(count) {
		$scope.pageList = [];
		var limit = 20; //返回的条数
		$scope.totalPage = parseInt(count / limit); //总页数
		$scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
		var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
		var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

		$scope.frontdotShow = start > 1 ? true : false; //前面的省略号
		$scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
		for (var i = start; i <= end; i++) {
			$scope.pageList.push({
				number: i,
				active: i == $scope.currentPage ? 'am-active' : ''
			}); //给点击的按钮添加class
		}
	};
}
function detailController($scope,$routeParams,$window,$location,$http){
	$scope.getOrderInfo=function(){
	    var data = {
	        'order_id':$routeParams.order_id
	    };
	    baas_yl.call({
	        'http': $http,
	        'method': 'post',
	        'data':data,
	        'uri': 'order/orderList/info',
	        'success': function(d) {
	           $scope.order = d.orderinfo;
	           //console.log(d.orderinfo);
		        $scope.dubanlist = d.dubanlist;//督办记录
		        $scope.companylist = d.companylist;//受理企业列表
		        $scope.historylist = d.historylist;  //订单操作记录
		        if(typeof $scope.order.company != 'undefined'){
		            for( var i = 0; i < $scope.companylist.length; i ++ ){
		                for( var j = 0; j < $scope.order.company.length; j ++ ){
		                    if( $scope.companylist[i].name == $scope.order.company[j] ){
		                        $scope.companylist[i].chk = true;
		                    }
		                }
		            }
		        }
	        }
	    });
	}
	$scope.getOrderInfo();
}
